home *** CD-ROM | disk | FTP | other *** search
Text File | 1989-06-26 | 6.1 KB | 138 lines | [TEXT/GEOL] |
- Item 4354982 3-June-89 15:10
-
- From: MOOF Rollin, Keith A, APL
-
- To: MACAPP.TECH$ MACAPP Tech
-
- Sub: Re: Comparing Objects
-
- Alan,
-
- I'd like to address your idea on TObject.Compare. Keep in mind that I'm not one
- of the MacApp engineers, that these opinions are my own, and that they don't
- represent those of the MacApp programmers; I have no idea what merit they will
- find in your idea.
-
- Overall, I'm afraid that I disagree with your approach. I think that the
- default comparison routine will tend to cause confusion, and I also think that
- a Compare routine is not appropriate for the base TObject object. I'll explain
- all of this below in comments interspersed with yours.
-
- First of all, I think that your default compare method will cause more harm
- than good. Better would be for the Compare method to do nothing at all than
- what you suggest:
-
- > FUNCTION TObject.Compare(anObject:TObject):INTEGER;
- > VAR p,q:LONGINT;
- > BEGIN
- > p:= LONGINT(SELF);
- > q:= LONGINT(anObject);
- > IF p < q THEN
- > Compare:= kALessThanB
- > ELSE IF p > q THEN
- > Compare:= kAGreaterThanB
- > ELSE { p = q }
- > Compare:= kAEqualB;
- > END;
-
- I feel that your method is not consistant. In order for a sort comparison to be
- of any use, I feel that it should produce consistant sorts and results every
- time. The way you have it set up, compares will be at the mercy of the Memory
- Manager, as it hinges on the location of the object's Master Pointer in memory.
- There is no guarantee that this location will be same every time you run your
- program.
-
- > Such an implementation would require a minimal adjustment by making the
- > UList.p constants global and defaulting TSortedList.Compare(item1,item2) to
- > item1.Compare(item2).
- >
- >It would also allow easy compare methods for specialized objects. For example,
- > if someone had Finder-styled named objects, i.e. objects with a string field,
- > say TStrObject, one could override this Compare to produce
- >
- > FUNCTION TStrObject.Compare(anObject:TObject):INTEGER;OVERRIDE;
- > VAR p,q:Str255;
- > BEGIN
- > p:= TStrObject(SELF).fString;
- > q:= TStrObject(anObject).fString;
- > IF p < q THEN
- > Compare:= kALessThanB
- > ELSE IF p > q THEN
- > Compare:= kAGreaterThanB
- > ELSE { p = q and the user didn't provide unique names }
- > Compare:= INHERITED Compare(anObject);
- > END;
-
- Actually, I think that the current method of comparing in TSortedList is better
- for the scenario you present. Let's look at it another way. As you know, you
- can sort in different ways in the Finder windows. Two implement this, you could
- take the following two approaches:
-
- - have a case statement in your TYourSortedList.Compare routine that
- looked at the type of sorting required (possibly stored in an
- fSortType field) and make the appropriate comparisons.
- - store a pointer to a comparison routine in an fSortProcPtr field of your
- TYourSortedList object. The TYourSortedList.Compare routine could then
- just call that routine. All you would have to do to change the
- comparison criteria would be to change the pointer in that field to
- some other global procedure.
-
- On the whole, I think that it is better to have a list that knows something
- about objects, rather than an object that knows about lists.
-
- >Perhaps the default TSortedList.Compare could even call, in debug mode,
- >something like GetEltType(item1).Compare(item2) so that such lists always have
- >a default sorting which, of course, can be still overridden.
-
- I'm not quite sure what you're trying to do here. There is no GetEltType method
- that I know of, and even if there is, it seems to me that it would return an
- ObjClassID, not an object.
-
- > My rationale for this is that Compare seems to be a property of objects, not
- > the lists which contain them; and that the above is a canonical content-free
- > way to compare such, which has the crucial property that aItem.Compare(bItem)
- > = kAEqualB if and only if aItem = bItem. Overrides can easily produce content
- > sensitive versions of this: aItem.Compare(bItem) = kAEqualB if and only if
- > "Content"(aItem) = "Content"(bItem) as in the TStrObject version above.
-
- I'm afraid that I disagree with you here. Objects at a base level should not
- know about other objects. That is the whole point of object oriented
- programming. However, it is recognized that relationships must be maintained
- somewhere are sometime, so TLists and TSortedLists were created. It is the sole
- function of these objects to maintain those relationships, and, hence, it is
- they that should contain the comparison criteria.
-
- This has the advantage mentioned above that the comparison criteria can be
- changed by merely changing the list that the items are in. Otherwise, you will
- have to create a subclass for every type of comparison you want to support.
-
- > Since I do not have 2.0ß9, I do not know if such a method has been
- > implemented. Since my in-depth knowledge of programming philosophy and
- > practices is limited, I cannot estimate all the ramifications of such a new
- > method. I would like to suggest this method though, and if the idea is dumb
- > find out why. [because I am seriously considering tweaking my MacApp to
- > include this method]
- >
- > If this idea is reasonable, the minimal code and documentation changes
- > required might make it a reasonable last minute inclusion for 2.0ß9. If
- > deadlines are long past [and the idea holds water], could it be considered
- > for the next release?
- >
- > Alan CDA0373
- >
-
- Thanks for your ideas on this Alan. I'm sure that if others feel this way, then
- we'll soon have a lively discussion on this in MacApp.Tech$. For instance, I do
- think that a strong case could be made of a TObject.IsSame method that will
- determine if two objects are the same. A slightly shakier case could be made
- for TObject.IsEqual, as long as you compared the entire object, and not just
- some arbitrary key.
-
- In the meantime, let us know if you have any other suggestions. All are welcome
- for MacApp 2.1!
-
- - Keith Rollin
- - Apple Developer Technical Support
-
-
-